home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODF-Interest Archive / July 96 / Using Counted Ptr objects and C < prev    next >
Encoding:
Internet Message Format  |  1996-07-15  |  2.2 KB  |  [TEXT/ttxt]

  1. Subject:     Using Counted Ptr objects and Collections?
  2. Sent:        7/15/96 2:05 AM
  3. Received:    7/15/96 8:26 AM
  4. From:        Itrat Khan, khan@mustang.uwo.ca
  5. Reply-To:    ODF Interest, ODF-Interest@CILabs.ORG
  6. To:          OpenDoc Development Framework Discussion List, ODF-Interest@CILabs.
  7.  
  8. I have a question about using pointer-based reference counted objects
  9. and ordered collections. I admit I don't know my C++ operator
  10. overloading rules extremely well, so maybe there's a simple solution
  11. to this problem. :)
  12.  
  13. I'm using an object that I wish to be both reference-counted (pointer
  14. based using FW_TCountedPtr) and part of an ordered collection
  15. (FW_TOrderedCollection). Here are the problems I run into (pretend my
  16. object's type is called CMyClass):
  17.  
  18. • FW_TOrderedCollection methods such as AddFirst() require me to
  19. provide a pointer to an object such as (CMyClass* element).
  20.  
  21. • The overloaded -> operator for FW_TCountedPtr won't work with a
  22. pointer to an object. The object has to be declared as (CMyClass
  23. element).
  24.  
  25. So I'd have to do something like this:
  26.     
  27.     // ----- Add some arbitrary element to the front of the list. -----
  28.     CMyClass*  element = new CMyClass();
  29.     orderedCollection->AddFirst(element);
  30.  
  31.     // ----- Later,  retrieve the element and work with it. -----
  32.     element = orderedCollection->First();
  33.     (*element)->GetInfoOrSomething();   // This is what gets messy.
  34.  
  35. OR
  36.  
  37.     // ----- Add some arbitrary element to the front of the list. -----
  38.     CMyClass  element;
  39.     orderedCollection->AddFirst(&element);   // This …
  40.  
  41.     // ----- Later,  retrieve the element and work with it. -----
  42.     element = * orderedCollection->First();   // … and this are
  43. messy.
  44.     element->GetInfoOrSomething();
  45.  
  46. I know the first scenario works and I guess the second will too, I'm
  47. just wondering if this is the only way I can use these subsystems
  48. together or if there's a simpler way. I know this is a C++ question,
  49. but can I overload the -> operator so it would work as follows:
  50.  
  51.     CMyClass* element;
  52.     element->GetInfoOrSomething();
  53.  
  54. and behind the scenes it really translates the last line to:
  55.  
  56.     element->fRep->GetInfoOrSomething();
  57.  
  58. I'd appreciate any suggestions, otherwise I'll just use one of the
  59. above two methods (yuck!) :)
  60.  
  61. Itrat.
  62. --
  63.  
  64.